package com.yoomiito.app.ui.account;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.rxbus.RxBus;
import com.umeng.socialize.UMShareAPI;
import com.yoomiito.app.Constant;
import com.yoomiito.app.MainActivity;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.model.bean.GuiMIHuiBean;
import com.yoomiito.app.model.bean.UserInfo;
import com.yoomiito.app.present.account.GuiMiHuiLoginPresent;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.EditTextUtil;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.RxBusManager;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.widget.dialog.CustomDialog;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.router.Router;


/**
 * 1.手机号码正常登陆
 * <p>
 * 2、微信登陆
 */
public class GuiMIHuiLoginActivity extends BaseActivity<GuiMiHuiLoginPresent> {

    @BindView(R.id.act_guimihui_id_tv)
    EditText idEt;
    @BindView(R.id.act_gmh_bind)
    Button bindBtn;
    @BindView(R.id.iv_delete)
    ImageView iv_delete;
    @BindView(R.id.iv_back_left)
    ImageView backIv;
    private EditTextUtil mEditTextUtil;
    private int mLoginType;
    private CustomDialog mCustomDialog;

    @Override
    public int getLayoutId() {
        return R.layout.act_guimihui_login;
    }

    @Override
    public GuiMiHuiLoginPresent newP() {
        return new GuiMiHuiLoginPresent(App.mAppConfig);
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        mLoginType = getIntent().getIntExtra(Constant.TYPE, AllCode.Login);
        mEditTextUtil = new EditTextUtil(idEt, bindBtn);
        mEditTextUtil.setNumberLength(1);
        mEditTextUtil.setIvDelete(iv_delete);
    }


    @Override
    protected void onResume() {
        super.onResume();
        mEditTextUtil.setEditTextListener();
    }

    @OnClick({R.id.iv_back_left,R.id.act_gmh_bind,  R.id.iv_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back_left:
                finish();
                break;
            case R.id.act_gmh_bind://
                String id = idEt.getText().toString().trim();
                if (TextUtils.isEmpty(id)){
                    showToast("请输入瑰秘汇ID");
                    return;
                }
                getP().sureId(id);
                break;
            case R.id.iv_delete://清空输入框
                idEt.setText("");
                break;
        }
    }

    public void showId(GuiMIHuiBean bean){
        if (mCustomDialog == null) {
            mCustomDialog = new CustomDialog(this, new CustomDialog.OnCloseListener() {

                @Override
                public void confirm() {
                    mEditTextUtil.deleteTextWatcher();
                    Router.newIntent(GuiMIHuiLoginActivity.this)
                            .to(BindingPhoneActivity.class)
                            .putInt(Constant.KEY,AllCode.GuiMiHui)
                            .putString(Constant.DATA,bean.getActivation_code())
                            .launch();
                }
            });
            mCustomDialog.setTitle("请确认你的瑰秘汇ID");
            mCustomDialog.setCanceledOnTouchOutside(true);
        }
        mCustomDialog.setContent(bean.getNickname());
        mCustomDialog.setHead(bean.getHeadimgurl());
        mCustomDialog.setContent_1(bean.getActivation_code());
        mCustomDialog.show();
    }

    /**
     * 去往主界面
     */
    public void toMainActivity(UserInfo userInfo) {
//        CurrentUser.mUserInfo = userInfo;
//        SharedPref.getInstance(this).putBoolean(Constant.IS_LOGIN,true);
//        SharedPref.getInstance(this).putString(Constant.TOKEN, userInfo.getToken());
        UserInfoConfig.loginPutUserConfig(userInfo,true);
        Router.newIntent(context)
                .to(MainActivity.class)
                .launch();

    }

    @Override
    protected void onDestroy() {
        mEditTextUtil.deleteTextWatcher();
//        RxBusManager.unregisterRxBusManager(this);
        super.onDestroy();
    }

    @Override
    public void eventMessage(EventMessage eventMessage) {
        super.eventMessage(eventMessage);
        if (AllCode.LoginSuccess.equals(eventMessage.getEventType())){
            this.finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onBackPressed() {
        if (AllCode.ExpLogin!=mLoginType){
            super.onBackPressed();
        }
    }
}
